﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Interfleet.SE.UICompiler.Core
{
    /// <summary>
    /// An implementation of the <see cref="IFileProvider"/> interface.
    /// </summary>
    internal class FileProvider : IFileProvider
    {
        #region IFileProvider Members

        string IFileProvider.ReadFileToEnd(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return string.Empty;
            }
            using (StreamReader sr = new StreamReader(filePath))
            {
                return sr.ReadToEnd();
            }
        }

        void IFileProvider.SaveToFile(string filePath, string fileContent)
        {
            using (var sr = new StreamWriter(filePath, false, Encoding.UTF8))
            {
                sr.Write(fileContent);
                sr.Flush();
            }
        }

        long IFileProvider.GetFileSize(string filePath)
        {
            return new FileInfo(filePath).Length;
        }

        DateTime IFileProvider.GetLastWriteTime(string filePath)
        {
            return new FileInfo(filePath).LastWriteTime;
        }

        bool IFileProvider.FileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        #endregion
    }
}
